﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.InterfaceServer;

namespace ZK.YHP_TransManage
{

    public partial class Index : Form
    {
        private WFAutoSizeHelper wFAutoSizeHelper = new WFAutoSizeHelper();
        private UC.PrintPoundList _PrintPoundList;

        /// <summary>
        /// 25分钟登录一次，避免token过期。
        /// </summary>
        System.Timers.Timer tokenTimer =
        new System.Timers.Timer(1500000);
        byte NowPageIndex = 255;

        public Index()
        {
            InitializeComponent();
            wFAutoSizeHelper.GetAllInitInfo(this.Controls[0], this);
            this.StartPosition = FormStartPosition.CenterScreen;

            HideShowMenu();

            labelLCw.Text = IConfig.GetSetuserInfo.UserName;

            tokenTimer.Elapsed += TokenTimer_Elapsed;
            tokenTimer.Enabled = true;
            tokenTimer.AutoReset = false;
            tokenTimer.Start();
        }
        /// <summary>
        /// 判断是否当前页
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        bool JudgeNowPage(byte i)
        {
            if (NowPageIndex == i)
            {
                return true;
            }
            else
            {
                NowPageIndex = i;
                return false;
            }
        }
        private void TokenTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                Models.LoginUserM loginUserM = new Models.LoginUserM();
                var xmlUerInfo = Common.IConfig.GetSetuserInfo;
                loginUserM.username = xmlUerInfo.LoginAccount;
                loginUserM.password = xmlUerInfo.PassWord;
                Models.ReceiveDataBase<Models.LoginReceiveM> receiveDataBase = LoginApiServer.Login(loginUserM);
                if (receiveDataBase == null || !receiveDataBase.success)
                {
                    Common.Log4.LogManage.WriteErr(" TokenTimer_Elapsed 登录失败");
                }
                else
                {
                    IConfig.GetSetuserInfo = new IConfig.UserInfo()
                    {
                        LoginAccount = xmlUerInfo.LoginAccount,
                        PassWord = xmlUerInfo.PassWord,
                        Token = receiveDataBase.data.deptList[0].token
                    };
                }
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
            tokenTimer.Start();
        }

        /// <summary>
        /// 显示隐藏相关的菜单列表
        /// </summary>
        private void HideShowMenu()
        {
            string item = AppConfigHelper.GetAppSettings("MenuItem");
            var itemArr = item.Split(',');
            if (item.Length > 0)
            {
                foreach (var i in item)
                {
                    if (i == '1')
                    {
                        this.panel2.Visible = false;
                    }
                    if (i == '2')
                    {
                        this.panel3.Visible = false;
                    }
                    if (i == '3')
                    {
                        this.panel4.Visible = false;
                    }
                    if (i == '4')
                    {
                        this.panel5.Visible = false;
                    }
                    if (i == '5')
                    {
                        this.panel6.Visible = false;
                    }
                    if (i == '6')
                    {
                        this.panel7.Visible = false;
                    }


                }
            }
        }

        /// <summary>
        /// 窗口变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (wFAutoSizeHelper.ControlsInfo.Count > 0)//如果字典中有数据，即窗体改变
            {
                wFAutoSizeHelper.ControlsChangeInit(this.Controls[0]);//表示pannel控件
                wFAutoSizeHelper.ControlsChange(this.Controls[0]);
                Common.DelegateHelper.SendWidthHeightEvent?.Invoke(this.Controls[0].Width, this.Controls[0].Height);
            }
        }

        /// <summary>
        /// 改变左边菜单栏样式
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="button"></param>
        private void ChangeStyle(byte inex)
        {
            var panels = panelMenu.Controls;
            Panel nowPanel = new Panel();
            Label nowLabel = new Label();
            foreach (Panel item in panels)
            {
                item.BackColor = Color.FromArgb(41, 66, 101);
                var label = item.Controls[1];
                label.ForeColor = Color.FromArgb(154, 168, 192);
                if (panels.IndexOf(item) == inex)
                {
                    nowPanel = item;
                    nowLabel = (Label)item.Controls[1];
                }
            }

            nowPanel.BackColor = Color.FromArgb(28, 50, 79);
            nowLabel.ForeColor = Color.FromArgb(221, 228, 240);

            //wFAutoSizeHelper.GetAllInitInfo(this.Controls[0], this);
            DelegateHelper.SendWidthHeightEvent?.Invoke(this.Controls[0].Width, this.Controls[0].Height);
        }


        /// <summary>
        /// cancel button event
        /// 注销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            //注销
            //this.Hide();
            //Login login = new Login();
            //login.Show();
            if (MessageBox.Show("确定注销？", "提示", MessageBoxButtons.YesNoCancel,
              MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Login login = new Login();
                login.InitName = false;
                login.ControlBox = false;
                login.MinimizeBox = false;
                //var a = login.Controls["panel1"].Controls["textBoxUserName"];
                //a.Text = "asd";
                //login.Controls["panel1"].Controls["textBoxPassWord"].Text = "";
                login.ShowDialog();
                labelLCw.Text = IConfig.GetSetuserInfo.UserName;


            }
        }

        /// <summary>
        /// closed event  关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Index_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// exit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonReset_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("确定退出程序？", "提示", MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Thread.Sleep(1000);
                Application.Exit();
            }
        }

        /// <summary>
        /// 轻磅过磅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel2_Click(object sender, EventArgs e)
        {
            if (JudgeNowPage(0))
            {
                return;
            }
            ChangeStyle(0);
            panelContent.Controls.Clear();
            UC.LightWeight wagonBalance = new UC.LightWeight
            {
                Parent = panelContent,
                Dock = DockStyle.Fill
            };
        }
        /// <summary>
        /// 重磅过磅
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel3_Click(object sender, EventArgs e)
        {
            if (JudgeNowPage(1))
            {
                return;
            }
            ChangeStyle(1);
            panelContent.Controls.Clear();
            UC.HeavyWeight wagonBalance = new UC.HeavyWeight
            {
                Parent = panelContent,
                Dock = DockStyle.Fill
            };

            var ct = panelMenu.Controls;

        }
        /// <summary>
        /// 出矿
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel4_Click(object sender, EventArgs e)
        {
            if (JudgeNowPage(2))
            {
                return;
            }
            ChangeStyle(2);
            panelContent.Controls.Clear();
            YHP_TransManage.UC.Out printPoundList = new YHP_TransManage.UC.Out
            {
                Parent = panelContent,
                Dock = DockStyle.Fill
            };
            printPoundList.Show();
            var ct = panelMenu.Controls;
        }
        /// <summary>
        /// 筒仓管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel5_Click(object sender, EventArgs e)
        {
            if (JudgeNowPage(3))
            {
                return;
            }
            ChangeStyle(3);
            panelContent.Controls.Clear();
            YHP_TransManage.UC.Silo printPoundList = new YHP_TransManage.UC.Silo
            {
                Parent = panelContent,
                Dock = DockStyle.Fill
            };
            printPoundList.Show();
            var ct = panelMenu.Controls;
        }
        /// <summary>
        /// 打印磅单 弹窗无需控制是否当前页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel6_Click(object sender, EventArgs e)
        {
            ChangeStyle(4);
            if (_PrintPoundList == null)
            {
                _PrintPoundList = new UC.PrintPoundList();
                _PrintPoundList.Show();

            }
            else
            {
                if (_PrintPoundList.IsDisposed) //若子窗体关闭 则打开新子窗体 并显示
                {
                    _PrintPoundList = new UC.PrintPoundList();
                    _PrintPoundList.Show();
                }
                else
                {
                    _PrintPoundList.Activate(); //使子窗体获得焦点
                }
            }
            var ct = panelMenu.Controls;
        }

        private void panel7_Click(object sender, EventArgs e)
        {
            if (JudgeNowPage(5))
            {
                return;
            }
            ChangeStyle(5);
            panelContent.Controls.Clear();
            YHP_TransManage.UC.InCoal printPoundList = new YHP_TransManage.UC.InCoal
            {
                Parent = panelContent,
                Dock = DockStyle.Fill
            };
            printPoundList.Show();
            var ct = panelMenu.Controls;
        }
    }
}
